<?php
class SurveyController extends DefaultController{
	
	public function __construct(){
		$departmentList = DepartmentModel::getList();
		$tree = array();
		mkTree($departmentList, $tree);
		$this->assign('departmentList', $tree);
		$survey = require_once GLOFT_PATH . 'conf/survey.php';
		$this->assign('survey', $survey);
		parent::__construct();
	}
	
	public function _default(){
		
		$id = request('department_id');
		$department = array();
		if (!$id){
			$ip = get_client_ip();
			
			$ipInfo = IpBusiness::calcIp($ip);
			if (isset($ipInfo['department_id']) && $ipInfo['department_id']){
				$department = DepartmentModel::get($ipInfo['department_id']);
			}
		}else{
			$department = DepartmentModel::get($id);
		}
		
		if (!$department){
			$this->_404('您没有投票的权限');
		}
		
		
		$this->assign('department', $department);
		
		$this->display();
	}
	
	public function stat(){
		$id = request('department_id');
		if (!$id) {
			$this->_404();
		}
		
		$stat = SurveystatModel::getLast($id);
		
		if (!$stat){
			$this->_404();
		}
		
		$this->assign('stat', $stat);
		$this->display();
	}
}